{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入路径：F:\\数据分析\\实验2\n",
      "路径下文件夹不存在，是否需要创建？(输入1表示创建，0表示不创建)：1\n",
      "路径下所有文件名：['File0.txt', 'File1.txt', 'File2.txt', 'File3.txt', 'File4.txt', 'File5.txt', 'File6.txt', 'File7.txt', 'File8.txt', 'File9.txt']\n",
      "文件管理程序结束，是否删除当前练习文件？(输入1代表删除，0代表保留)：1\n",
      "文件已删除\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "def delete_folder_contents(folder_path):\n",
    "    # 递归删除文件夹内所有文件和文件夹\n",
    "    for item in folder_path.iterdir():\n",
    "        if item.is_file():  # 如果当前项目是文件\n",
    "            item.unlink()   # 删除文件\n",
    "        else:\n",
    "            delete_folder_contents(item)  # 递归调用，删除子文件夹内所有内容\n",
    "    folder_path.rmdir()  # 删除空文件夹\n",
    "\n",
    "def create_files(path):\n",
    "    input_path = Path(path)\n",
    "    \n",
    "    if not input_path.exists():  # 检查路径是否存在\n",
    "        create_folder = int(input(\"路径下文件夹不存在，是否需要创建？(输入1表示创建，0表示不创建)：\"))\n",
    "        if create_folder == 1:\n",
    "            input_path.mkdir(parents=True)  # 创建文件夹及其父文件夹（如果不存在）\n",
    "        else:\n",
    "            return\n",
    "\n",
    "    # 创建10个文件并写入内容\n",
    "    for i in range(10):\n",
    "        file_name = f\"File{i}.txt\"\n",
    "        with open(input_path / file_name, \"w\", encoding='utf-8') as file:\n",
    "            file.write(f\"python pathlib {i}\")\n",
    "\n",
    "    files_in_path = [file.name for file in input_path.iterdir() if file.is_file()]  # 获取路径下所有文件名\n",
    "    print(f\"路径下所有文件名：{files_in_path}\")\n",
    "\n",
    "    copy_folder = input_path / \"Copy\"\n",
    "    try:\n",
    "        copy_folder.mkdir()  # 创建一个名为Copy的文件夹\n",
    "    except FileExistsError:\n",
    "        print(\"文件夹已存在\")\n",
    "\n",
    "    # 复制文件到Copy文件夹，并改变文件名\n",
    "    for file_name in files_in_path:\n",
    "        content = (input_path / file_name).read_text(encoding='utf-8')\n",
    "        new_file_name = f\"{file_name[0:5]}Copy{file_name[5:]}\"\n",
    "        (copy_folder / new_file_name).write_text(content, encoding='utf-8')\n",
    "\n",
    "    delete_files = int(input(\"文件管理程序结束，是否删除当前练习文件？(输入1代表删除，0代表保留)：\"))\n",
    "    if delete_files == 1:\n",
    "        delete_folder_contents(input_path)  # 调用函数删除当前路径下所有文件和文件夹\n",
    "        print(\"文件已删除\")\n",
    "    else:\n",
    "        print(\"文件保留\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    input_path = input(\"请输入路径：\")\n",
    "    create_files(input_path)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
